window.addEventListener("load", function() {
    class Login {
        constructor() {
            this.cli()
                // this.tiao()
            this.usermsg = JSON.parse(sessionStorage.getItem("usermsg"));
            console.log(this.usermsg);
            if (this.usermsg !== null) {
                this.writeUserMsg();
            }
            this.bindEvent()
        }
        writeUserMsg() {
            $(".name").attr("value", this.usermsg.username)
            $(".password").attr("value", this.usermsg.password)
        }
        bindEvent() {
            $(".loginn").on("click", () => {
                this.login((res) => {
                    console.log(res);
                    switch (res.data.code) {
                        case 1:
                            this.success(res);
                            break;
                        case 0:
                            this.error(res);
                            break;
                    }
                })

            })
        }
        login(callback) {
            let res = axios.post("http://127.0.0.1:8888/users/login", `username=${$(".name").val()}&password=${$(".pd").val()}`)
                .then(res => {
                    // console.log(res);
                    callback(res)
                })
        }
        success(res) {
            alert("登录成功")
            setTimeout(() => {
                cookie("TOKEN", res.data.token);
                cookie("ID", res.data.user.id);
                location.href = "./";

            }, 2000);
        }
        error(res) {
            alert("登录失败")


        }

        cli() {
            $(function() {
                $(".a").on("click", function() {
                    $(".boxx").css({ "display": "block" })
                })
                $(".bb").on("click", function() {
                    $(".boxx").css({ "display": "none" })
                })
            })
        }

    }
    new Login()
})